<?php


namespace Tbf\Component\Http\ServeFile;


class FileMap implements FileMapInterface{
    protected $prefix_url ;
    protected $prefix_file_path ;
    function __construct($url='',$file_path=''){
        $this->prefix_url = $url;
        $this->prefix_file_path = $file_path;
    }
    function addUrlPrefix($prefix)
    {
        $this->prefix_url = $prefix.$this->prefix_url;
        return $this;
    }

    function addFilePrefix($prefix)
    {
        $this->prefix_file_path = $prefix.$this->prefix_file_path;
        return $this;
    }

    function addBothPrefix($prefix)
    {
        return $this->addUrlPrefix($prefix)
                    ->addFilePrefix($prefix);
    }

    function findFileByUrl($url)
    {
        if ($url !== $this->prefix_url){
            return array(null,'Not Handle');
        }
        return array($this->prefix_file_path,null);
    }
    function getPrefixUrl(){
        return $this->prefix_url;
    }
    protected function parseUrl($url){
        if (empty($this->prefix_url)){
            return array($url,null);
        }
        if (strpos($url,$this->prefix_url)!==0){
            return array(null,'Not Handle');
        }
        $url = substr($url,strlen($this->prefix_url));
        return array($url,null);
    }
}